﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressUserBusinessLayer;
using AlexandriaPressUserObject;

namespace AlexandriaPressWebsite.MasterPages.UserPages
{
    public partial class ShoppingCart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["blocked"] != null)
            {
                Session.RemoveAll();
                Response.Redirect("~/MasterPages/MainPage.aspx");
            }

            if (!Page.IsPostBack)
            {
                decimal sum = 0;
                ShoppingCartView.EmptyDataText = "There are currently no items in your shopping cart.";
                ShoppingCartView.DataSource = UserBusinsessLayer.getAllOrders((List<OrderItems>)Session["newOrders"], ref sum);
                ShoppingCartView.DataBind();
                if (sum == 0)
                {
                    totalPrice.Text = "Total Price: 0 pesos";
                }
                else
                {
                    totalPrice.Text = "Total Price: " + Convert.ToString(sum) + " pesos";
                }
            }

        }

        protected void Page_UnLoad(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                Session.Remove("newOrders");
            }

            if (Session["tempOrder"] != null)
            {
                Session["newOrders"] = Session["tempOrder"];
                Session.Remove("tempOrder");
            }
        }

        protected void ProceedToCheckOutButton_Click(object sender, EventArgs e)
        {
                List<OrderItems> savedItems = (List<OrderItems>)Session["newOrders"];
                foreach (OrderItems item in savedItems)
                {
                    if (item.Quantity == 0)
                    {
                        savedItems.Remove(item);
                    }
                }
                Session["newOrders"] = Session["orders"];
                Session.Remove("orders");
                Session.Add("orders", savedItems);
                Session["tempOrder"] = Session["newOrders"];
                Response.Redirect("~/MasterPages/UserPages/Checkout.aspx");
        }

        protected void ShoppingCartView_RowCancellingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            decimal sum = 0;
            ShoppingCartView.EmptyDataText = "There are currently no items in your shopping cart.";
            ShoppingCartView.EditIndex = -1;
            ShoppingCartView.DataSource = UserBusinsessLayer.getAllOrders((List<OrderItems>)Session["newOrders"], ref sum);
            ShoppingCartView.DataBind();
            if (sum == 0)
            {
                totalPrice.Text = "Total Price: 0 pesos";
            }
            else
            {
                totalPrice.Text = "Total Price: " + Convert.ToString(sum) + " pesos";
            }
        }

        protected void ShoppingCartView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            decimal sum = 0;
            ShoppingCartView.EmptyDataText = "There are currently no items in your shopping cart.";
            ShoppingCartView.EditIndex = e.NewEditIndex;
            ShoppingCartView.DataSource = UserBusinsessLayer.getAllOrders((List<OrderItems>)Session["newOrders"], ref sum);
            ShoppingCartView.DataBind();
            if (sum == 0)
            {
                totalPrice.Text = "Total Price: 0 pesos";
            }
            else
            {
                totalPrice.Text = "Total Price: " + Convert.ToString(sum) + " pesos";
            }
            Session["tempOrder"] = Session["newOrders"];
        }

        protected void ShoppingCartView_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            decimal sum = 0;
            ShoppingCartView.PageIndex = e.NewPageIndex;
            ShoppingCartView.DataSource = UserBusinsessLayer.getAllOrders((List<OrderItems>)Session["newOrders"], ref sum);
            ShoppingCartView.DataBind();
            if (sum == 0)
            {
                totalPrice.Text = "Total Price: 0 pesos";
            }
            else
            {
                totalPrice.Text = "Total Price: " + Convert.ToString(sum) + " pesos";
            }
            Session["tempOrder"] = Session["newOrders"];
        }

        protected void ShoppingCartView_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Label label = (Label)ShoppingCartView.Rows[e.RowIndex].FindControl("IDLabel");
            TextBox tBox = (TextBox)ShoppingCartView.Rows[e.RowIndex].FindControl("QuantityBox");
            int quantity = 0;
            int identifier = 0;
            decimal price = 0;
            int stock = 0;
            string title = "";
            ShoppingCartView.EditIndex = -1;
            UserBusinsessLayer.getDetails(Convert.ToInt32(label.Text), ref identifier, ref price, ref stock, ref title);
            foreach (OrderItems orderItems in (List<OrderItems>)Session["newOrders"])
            {
                if (orderItems.BookID == Convert.ToInt32(label.Text))
                {
                    orderItems.Quantity = Convert.ToInt32(tBox.Text);
                }
                quantity += orderItems.Quantity;
            }
            foreach (OrderItems orderItems in (List<OrderItems>)Session["newOrders"])
            {
                if (Convert.ToInt32(label.Text) == orderItems.BookID)
                {
                    if (Convert.ToInt32(tBox.Text) > stock)
                    {
                        totalPrice.Text = "The number of items you ordered exceeds the total in stock for that item.";
                    }
                    break;
                }
            }
            decimal sum = 0;
            ShoppingCartView.DataSource = UserBusinsessLayer.getAllOrders((List<OrderItems>)Session["newOrders"], ref sum);
            ShoppingCartView.DataBind();
            if (sum == 0)
            {
                totalPrice.Text = "Total Price: 0 pesos";
            }
            if (sum == 1)
            {
                totalPrice.Text = "Total Price: 1 peso";
            }
            if (sum > 1)
            {
                totalPrice.Text = "Total Price: " + Convert.ToString(sum) + " pesos";
            }

            HyperLink content = (HyperLink)this.Master.FindControl("cartLink");

            if (quantity == 0 )
            {
                content.Text = "Cart: 0 items";
            }

            if (quantity == 1)
            {
                content.Text = "Cart: 1 item";
            }

            if (quantity > 1)
            {
                content.Text = "Cart: " + Convert.ToString(quantity) + " items";
            }

            Session.Remove("numItems");
            Session.Add("numItems", quantity); 
            Session["tempOrder"] = Session["newOrders"];
        }
    }
}
